//
// ConfigModel
//
// Copyright (C) jextra.net.
//
//  This file is part of the Nubbin Library.
//
//  The Nubbin Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Nubbin Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Nubbin Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.nubbinstoreweb;

import java.io.*;
import java.util.*;
import net.jextra.nubbin.store.*;

public class ConfigModel
{
    // ============================================================
    // Fields
    // ============================================================

    public static final String PROP_NUBBINSTORE_ROOT_DIR = "nubbinstore.root.dir";

    private File _storeRootDir;

    private FileStore _store;

    // ============================================================
    // Constructors
    // ============================================================

    public ConfigModel()
        throws IOException
    {
        read();
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void read()
        throws IOException
    {
        _storeRootDir = null;
        _store = null;

        File propsFile = getPropsFile();
        if ( !propsFile.exists() )
        {
            return;
        }
        Properties props = new Properties();
        FileReader in = new FileReader( propsFile );
        props.load( in );
        in.close();

        String storeString = props.getProperty( PROP_NUBBINSTORE_ROOT_DIR );
        if ( storeString != null )
        {
            File dir = new File( storeString );
            if ( dir.exists() && dir.isDirectory() )
            {
                _storeRootDir = dir;
                _store = new FileStore( _storeRootDir );
            }
        }
    }

    public void save()
        throws IOException
    {
        File propsFile = getPropsFile();
        Properties props = new Properties();
        if ( _storeRootDir != null )
        {
            props.setProperty( PROP_NUBBINSTORE_ROOT_DIR, _storeRootDir.getAbsolutePath() );
        }
        
        FileWriter out = new FileWriter( propsFile );
        props.store( out, "" );
        out.close();

    }

    public File getStoreRootDir()
        throws IOException
    {
        return _storeRootDir;
    }

    public void setStoreRootDir( File dir )
    {
        _storeRootDir = dir;
    }

    public boolean isStoreRootDirValid()
        throws IOException
    {
        File dir = getStoreRootDir();
        if ( dir == null )
        {
            return true;
        }

        return dir.exists();
    }

    public FileStore getStore()
    {
        return _store;
    }

    // ----------
    // private
    // ----------

    private File getPropsFile()
        throws IOException
    {
        String catHome = System.getProperty( "catalina.home" );
        File confDir = new File( catHome, "conf" );
        confDir.mkdirs();
        File propsFile = new File( confDir, "nubbinstoreweb.properties" );

        return propsFile;
    }
}

